package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.OrderWorkRedistributeOperator;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeApplyService;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeApplyBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 类描述: 改派相关消息的监听处理类
 *
 * @author: HuangChao
 * @since: 2022/06/01 11:45
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class RedistributeListener {

    @Autowired
    private OrderWorkRedistributeApplyService orderWorkRedistributeApplyService;
    @Autowired
    private OrderWorkRedistributeApplyBService orderWorkRedistributeApplyBService;

    /**
     * 改派申请取消监听
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_WORK_REDISTRIBUTE_CANCEL_TAG)
    public void redistributeCancel(String message){

        try {
            log.info("#mq#redistributeCancel# message:{}", message);
            JSONObject data = JSON.parseObject(message);
            Long orderId = data.getLong("orderId");
            Long workId = data.getLong("workId");
            Integer type = data.getInteger("type");

            String typeName = OrderLogConsts.getOrderLogTypeName(type);
            OrderWorkRedistributeOperator orderWorkRedistributeOperator = new OrderWorkRedistributeOperator();
            orderWorkRedistributeOperator.setOrderId(orderId);
            orderWorkRedistributeOperator.setWorkId(workId);
            orderWorkRedistributeOperator.setOperatorId(Long.valueOf(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
            orderWorkRedistributeOperator.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
            orderWorkRedistributeOperator.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            orderWorkRedistributeOperator.setOperateTime(DateUtil.getNow());
            orderWorkRedistributeOperator.setRedistributeOperatorLogRemark(typeName + "，系统自动撤销改派申请");
            orderWorkRedistributeApplyBService.autoCancelRedistributeApply(orderWorkRedistributeOperator);
        } catch (OmsBaseException ex) {
            log.warn("#mq#addcomplaint# {}", ex.getMessage());
        } catch (Exception e) {
            log.error("#mq#addcomplaint# {} ", e);
        }
    }

}
